package org.mte.swift.builder.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.mte.sakn.util.ListSet;
import org.mte.swift.builder.model.app.AppModel;
import org.mte.swift.builder.model.jpa.ColumnEntity;
import org.mte.swift.builder.model.jpa.Entity;
import org.mte.swift.builder.model.placeholder.Placeholder;
import org.mte.swift.builder.model.template.Template;

public class SwiftModel {
		
	protected String targetPackage = null;
	
	protected String sourcePath = null;
	
	protected String webPath = null;
	
	protected String entityPath = null;
	
	protected List<AppModel> appModels = null;
	
	protected ListSet<Template> templateSet = null;

	public SwiftModel() {
		
	}
	
	public String getTargetPackage() {
		return targetPackage;
	}

	public String getWebPath() {
		return webPath;
	}

	public void setTargetPackage(String targetPackage) {
		this.targetPackage = targetPackage;
	}

	public String getSourcePath() {
		return sourcePath;
	}

	public void setSourcePath(String workRoot, String sourcePath) {
		String relativePackage = targetPackage.replaceAll("\\.", "/");
		this.sourcePath = workRoot + sourcePath + relativePackage + "/";
	}
	 
	public String getEntityPath() {
		return entityPath;
	}

	public void setEntityPath(String workRoot, String entityPath) {
		this.entityPath = workRoot + entityPath + "/";
	}
	
	public void setWebPath(String workRoot, String webPath) {
		this.webPath = workRoot + webPath;
	}

	public List<AppModel> getAppModels() {
		return appModels;
	}

	/*public void setAppModels(List<AppModel> webModels) {
		this.appModels = webModels;
	}*/
	
	public void setAppModels(List<String> entityClassNames) {
		if (entityClassNames != null) {
			appModels = new ArrayList<AppModel>();
			for (String entityClassName : entityClassNames) {
				AppModel appModel = new AppModel();
				Entity entity = new Entity();
				int pos = entityClassName.lastIndexOf(".");
				String name = entityClassName.substring(pos + 1);
				entity.setName(name);
				entity.setLowName(name.toLowerCase());
				entity.setUpName(name.toUpperCase());
				entity.setClassName(entityClassName);
				appModel.setEntity(entity);
				appModels.add(appModel);
			}
		}
	}

	public ListSet<Template> getTemplateSet() {
		if (templateSet == null) {
			templateSet = new ListSet<Template>();
		}
		return templateSet;
	}
	
	public void addSourceTemplate(String templateName, String templateContent) {
		getTemplateSet().add(new Template(templateName,  templateContent, Template.JAVA_TEMPLATE_TYPE));
	}
	
	public void addWebTemplate(String templateName, String templateContent) {
		getTemplateSet().add(new Template(templateName, templateContent, Template.WEB_TEMPLATE_TYPE));
	}
	
	public String toString() {
		StringBuilder b = new StringBuilder();
		b.append("sourcePath=").append(sourcePath).append("\n");
		//b.append("webPath=").append(webPath).append("\n");
		b.append("entityPath=").append(entityPath).append("\n");
		b.append("targetPackage=").append(targetPackage).append("\n");
		if (appModels != null) {
			int i = 0;
			for (AppModel appModel : appModels) {
				Entity entity = appModel.getEntity(); 
				b.append("entity.name.").append(i).append("=").append(entity.getName()).append(" - ").append(entity.getLowName()).append("\n");
				b.append("entity.tableName.").append(i).append("=").append(entity.getTableName()).append("\n");
				b.append("entity.className.").append(i).append("=").append(entity.getClassName()).append("\n");
				if (entity.getColumnList() != null) {
					int j = 0;
					for (ColumnEntity ce : entity.getColumnList().getEntities()) {
						b.append("columnEntity.").append(i).append("-").append(j).append(".");
						b.append(ce.toString()).append("\n");
						j++;
					}
				}
				if (appModel.getPlaceholderMap() != null) {
					Iterator<Placeholder> iterator = appModel.getPlaceholderMap().keySet().iterator();
					while (iterator.hasNext()) {
						Placeholder placeholder = iterator.next();
						String value = appModel.getPlaceholderValue(placeholder);
						b.append(placeholder.key).append("-").append(i).append("=").append(value).append("\n");
					}
				}
				for (Template template : appModel.getTemplateSet().getEntities()) {
					b.append("template=").append(template.getPath()).append("\n");
/*					if (template.getPath().indexOf(".properties") != -1) {
						b.append(template.getContent());
					}*/
				}
				i++;
			}
		}		
		return b.toString();

	}
}
